import { FINode } from './FINode.js';

export class FISprite extends FINode {
  protected _x: number = 0;
  protected _y: number = 0;
  protected _r: number = 0;
  protected _sx: number = 1;
  protected _sy: number = 1;
  protected _ax: number = 0;
  protected _ay: number = 0;
  protected _w: number = 0;
  protected _h: number = 0;
  protected _visible: boolean = true;

  anchor(x: number, y: number) {
    this.ax = x;
    this.ay = y;
    return this;
  }
  move(x: number, y: number) {
    this.x = x;
    this.y = y;
    return this;
  }

  resize(w: number, h: number) {
    this.w = w;
    this.h = h;
    return this;
  }

  rotate(angle: number) {
    this.r = angle;
    return this;
  }

  scale(x: number, y: number = x) {
    this.sx = x;
    this.sy = y;
    return this;
  }

  get x() { return this._x; }
  set x(v) { this._x = v; }

  get y() { return this._y; }
  set y(v) { this._y = v; }

  get w() { return this._w; }
  set w(v) { this._w = v; }

  get h() { return this._h; }
  set h(v) { this._h = v; }

  get r() { return this._r; }
  set r(v) { this._r = v; }

  get sx() { return this._sx; }
  set sx(v) { this._sx = v; }

  get sy() { return this._sy; }
  set sy(v) { this._sy = v; }

  get ax() { return this._ax; }
  set ax(v) { this._ax = v; }

  get ay() { return this._ay; }
  set ay(v) { this._ay = v; }

  get visible() { return this._visible; }
  set visible(v) { this._visible = v; }

  setVisible(v: boolean) {
    this.visible = v;
    return this;
  }

  render(ctx: CanvasRenderingContext2D): void {
    if (!this._visible)
      return;
    ctx.save();
    ctx.translate(this._x, this._y);
    ctx.rotate(this._r);
    ctx.scale(this._sx, this._sy);
    this.draw(ctx);
    super.render(ctx);
    ctx.restore();
  }

  draw(ctx: CanvasRenderingContext2D): void {
    ctx.lineWidth = 1;
    ctx.strokeRect(-0.5, -0.5, this._w, this._h)
  }
}

